Code Embedded Programming

by Moritz Begle


Posted on March 30, 2013 at 15:00 PM


const int buttonPin = 3; // the pin that the pushbutton is attached to

const int ledPin = 7;

int buttonPushCounter = 0; // counter for the number of button presses

int buttonState = 1; // current state of the button

int lastButtonState = 1;

int brightness = 0; // how bright the LED is

int fadeAmount = 5;

unsigned long time = 0;

int loopedtime = 0;

void setup() {

pinMode (buttonPin, INPUT);

pinMode (ledPin, OUTPUT);

}

void loop() {

buttonState = digitalRead(buttonPin);

if(buttonState != lastButtonState){

if(buttonState==1)

buttonPushCounter ++ ;

}

if(buttonPushCounter % 4 == 0){

prog1(500);

}

else if (buttonPushCounter % 4 == 1){

prog1(2000);

}

else if(buttonPushCounter % 4 == 2) {

prog3(2000);

}

else if (buttonPushCounter % 4 == 3) {

digitalWrite(ledPin, HIGH);

}

lastButtonState = buttonState;

}

void prog1(int interval) {

time = millis();

loopedtime = time % interval;

if(loopedtime < interval/2)

digitalWrite(ledPin, HIGH);

else

digitalWrite(ledPin, LOW); }

void prog3(int interval){

time = millis();

loopedtime = time % interval;

brightness = map(loopedtime, 0, interval, -255,255);

if(brightness <0)

brightness = -brightness ;

analogWrite(ledPin, brightness);

}